using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Keep.Const;
using Keep.Model;
using Keep.Util;
using Serilog;

namespace Keep.Service.Impl;

public class MusicListServiceImpl : IMusicListService
{
    public bool AddMusicList(MusicList musicList)
    {
        List<MusicList> list = SelMusicList(new MusicList());
        if (list != null && list.Count > 0)
        {
            foreach (MusicList ml in list)
            {
                if (ml.name.Equals(musicList.name))
                {
                    Log.Error("列表名称已经被使用了" + musicList.name);
                    return false;
                }
            }
        }
        musicList.status = Static.Enable;
        int i = SqlSugarHelper.Db.Insertable(musicList).ExecuteCommand();
        return i > 0;
    }

    public bool DelMusicList(int id)
    {
        int i = SqlSugarHelper.Db.Updateable<MusicList>()
                .SetColumns(it => it.status == Static.Disable)
                .Where(lt => lt.id == id)
                .ExecuteCommand();
        return i > 0;
    }

    public bool EditMusicList(MusicList musicList)
    {
        int i = SqlSugarHelper.Db.Updateable<MusicList>()
               .SetColumnsIF(!String.IsNullOrEmpty(musicList.name), it => it.name == musicList.name)
               .Where(lt => lt.id == musicList.id)
               .ExecuteCommand();
        return i > 0;
    }

    public List<MusicList> SelMusicList(MusicList musicList)
    {
        var query = SqlSugarHelper.Db.Queryable<MusicList>();
        query.Where(lt => lt.status.Equals(Static.Enable));
        if (!String.IsNullOrEmpty(musicList.name))
        {
            query.Where(lt => lt.name.Contains(musicList.name));
        }
        query.OrderByDescending(lt => lt.id);
        return query.ToList();
    }

    public MusicList SelMusicListById(int id)
    {
        return SqlSugarHelper.Db.Queryable<MusicList>().Where(lt => lt.status.Equals(Static.Enable) && lt.id == id).Single();
    }
}
